<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>quick-cocos2d-x API Documents - framework.server.Session</title>
<link rel="stylesheet" href="luadocx-style.css" type="text/css" />
<link rel="stylesheet" href="luadocx-style-monokai.css" type="text/css" />
<script src="luadocx-highlight.min.js"></script>
<script type="text/javascript" charset="utf-8">hljs.initHighlightingOnLoad();</script>
</head>
<body>
  <div id="container">

    <table id="main">
      <tr>
        <td id="navigation">

          <h2>Links</h2>
          <ul>
            <li><a href="index.html">Index</a></li>
          </ul>

          <h2>Modules</h2>
          <ul>
            <li><a href="index.html">framework.init</a></li>
            <li><a href="framework.client.api.context.html">framework.client.api.Context</a></li>
            <li><a href="framework.client.api.eventprotocol.html">framework.client.api.EventProtocol</a></li>
            <li><a href="framework.client.api.gamenetwork.html">framework.client.api.GameNetwork</a></li>
            <li><a href="framework.client.api.gamestate.html">framework.client.api.GameState</a></li>
            <li><a href="framework.client.api.localize.html">framework.client.api.Localize</a></li>
            <li><a href="framework.client.api.store.html">framework.client.api.Store</a></li>
            <li><a href="framework.client.api.timer.html">framework.client.api.Timer</a></li>
            <li><a href="framework.client.api.gamenetwork.openfeint.html">framework.client.api.gamenetwork.OpenFeint</a></li>
            <li><a href="framework.client.audio.html">framework.client.audio</a></li>
            <li><a href="framework.client.crypto.html">framework.client.crypto</a></li>
            <li><a href="framework.client.device.html">framework.client.device</a></li>
            <li><a href="framework.client.display.html">framework.client.display</a></li>
            <li><a href="framework.client.init.html">framework.client.init</a></li>
            <li><a href="framework.client.luaj.html">framework.client.luaj</a></li>
            <li><a href="framework.client.luaoc.html">framework.client.luaoc</a></li>
            <li><a href="framework.client.network.html">framework.client.network</a></li>
            <li><a href="framework.client.scheduler.html">framework.client.scheduler</a></li>
            <li><a href="framework.client.transition.html">framework.client.transition</a></li>
            <li><a href="framework.client.ui.html">framework.client.ui</a></li>
            <li><a href="framework.server.mysqleasy.html">framework.server.MysqlEasy</a></li>
            <li><a href="framework.server.rediseasy.html">framework.server.RedisEasy</a></li>
            <li><a href="framework.server.serveraction.html">framework.server.ServerAction</a></li>
            <li><a href="framework.server.serverapp.html">framework.server.ServerApp</a></li>
            <li><strong>framework.server.Session</strong></li>
            <li><a href="framework.server.init.html">framework.server.init</a></li>
            <li><a href="framework.shared.modelbase.html">framework.shared.ModelBase</a></li>
            <li><a href="framework.shared.debug.html">framework.shared.debug</a></li>
            <li><a href="framework.shared.errors.html">framework.shared.errors</a></li>
            <li><a href="framework.shared.functions.html">framework.shared.functions</a></li>
          </ul>

        </td> <!-- navigation -->

        <td id="content">

          <h1>Module <code>framework.server.Session</code></h1>

          <!-- BEGIN module doc -->

          <div id="module_doc">


          </div>

          <!-- END module doc -->


          <!--  BEGIN functions index -->

          <h2>Functions</h2>
          <table class="function_list">


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:ctor">Session:ctor&nbsp;(app)</a></td>
              <td class="summary">Session 用于在服务端保持一个会话状态。</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:makeSessionId">Session:makeSessionId&nbsp;(seed)</a></td>
              <td class="summary">生成 SessionId</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:validateSessionId">Session:validateSessionId&nbsp;(sid)</a></td>
              <td class="summary">验证 SessionId 是否是有效</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:start">Session:start&nbsp;(sid)</a></td>
              <td class="summary">启动 session，如果失败返回 Error</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:getSessionId">Session:getSessionId&nbsp;()</a></td>
              <td class="summary">获得 SessionId</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:get">Session:get&nbsp;(key)</a></td>
              <td class="summary">获取指定的键值</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:getAll">Session:getAll&nbsp;()</a></td>
              <td class="summary">获取所有键值</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:set">Session:set&nbsp;(key,&nbsp;value)</a></td>
              <td class="summary">设定键值</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:destroy">Session:destroy&nbsp;()</a></td>
              <td class="summary">删除 session</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:refresh">Session:refresh&nbsp;()</a></td>
              <td class="summary">刷新 session 过期时间</td>
            </tr>


            <tr>
              <td class="name" nowrap>&nbsp;-&nbsp;&nbsp;<a href="#anchor_Session:save">Session:save&nbsp;()</a></td>
              <td class="summary">保存 session</td>
            </tr>


          </table>

          <!--  END functions index -->

          <br />
          <br />

          <!-- BEGIN functions details -->

          <h2>Functions</h2>
          <dl class="function">


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:ctor"></a>
              <h3>Session:ctor (app)</h3>
            </dt>

            <dd class="member_method">

<p>Session 用于在服务端保持一个会话状态。</p>

<p>在调用 Session:start() 和 Session:save() 之后，服务端会保存一组键值。
在处理客户端的后续请求时，只要客户端提供了 SessionId，服务端就可以通过 SessionId 获得这一组键值。</p>

<p>会话状态可以用来保存跨越多个请求的状态，减少客户端和服务端之间需要传递的数据，并且更好的保护用户的敏感信息。</p>

<h3>关于 SessionId</h3>

<p>SessionId 用于唯一标示一个存储在服务端的会话数据。</p>

<p><strong>创建 SessionId 的算法</strong></p>

<ul>
<li>取服务器当前时间</li>
<li>取 4 位随机数</li>
<li>取当前用户 ID</li>
<li>时间 + 随机数 + 种子，求 MD5 得到 SessionId</li>
</ul>

<p><strong>Session 的存储和有效期</strong></p>

<ul>
<li>首次使用新 SessionId 调用 Session:start() 及 Session:save() 后，
一组以 SessionId 为键名的键值会被保存在服务端，并设定一个有效期；</li>
<li>在下一次使用已有 SessionId 调用 Session:start() 时，这组键值会被读取出来放入 session 对象；</li>
<li>再次调用 Session:save() 或 Session:refresh() 会自动更新 session 的有效期。</li>
<li>如果调用 Session:destroy()，session 对象中的值和存储在服务端的键值会被立即删除。</li>
</ul>

<p><strong>SessionId 的安全性</strong></p>

<ul>
<li>目前简化实现，SessionId 的传输和存储没有进行加密操作。</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:makeSessionId"></a>
              <h3>Session:makeSessionId (seed)</h3>
            </dt>

            <dd class="member_method">

<p>生成 SessionId</p>

<p><strong>Parameters:</strong></p>

<ul>
<li>seed: 用于生成 SessionId 的种子</li>
</ul>

<p><strong>Returns:</strong></p>

<ul>
<li>新生成的 SessionId</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:validateSessionId"></a>
              <h3>Session:validateSessionId (sid)</h3>
            </dt>

            <dd class="member_method">

<p>验证 SessionId 是否是有效</p>

<p><strong>Parameters:</strong></p>

<ul>
<li>sid: 要验证的 SessionId</li>
</ul>

<p><strong>Returns:</strong></p>

<ul>
<li>验证通过: true</li>
<li>验证失败: false</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:start"></a>
              <h3>Session:start (sid)</h3>
            </dt>

            <dd class="member_method">

<p>启动 session，如果失败返回 Error</p>

<p><strong>Parameters:</strong></p>

<ul>
<li>sid: SessionId，如果未提供将自动生成一个</li>
</ul>

<p><strong>Returns:</strong></p>

<ul>
<li>成功: 无</li>
<li>失败: 返回 Error</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:getSessionId"></a>
              <h3>Session:getSessionId ()</h3>
            </dt>

            <dd class="member_method">

<p>获得 SessionId</p>

<p><strong>Returns:</strong></p>

<ul>
<li>SessionId</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:get"></a>
              <h3>Session:get (key)</h3>
            </dt>

            <dd class="member_method">

<p>获取指定的键值</p>

<p><strong>Parameters:</strong></p>

<ul>
<li>key: 键名</li>
</ul>

<p><strong>Returns:</strong></p>

<ul>
<li>键值</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:getAll"></a>
              <h3>Session:getAll ()</h3>
            </dt>

            <dd class="member_method">

<p>获取所有键值</p>

<p><strong>Returns:</strong></p>

<ul>
<li>包含所有键值的数组</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:set"></a>
              <h3>Session:set (key, value)</h3>
            </dt>

            <dd class="member_method">

<p>设定键值</p>

<p><strong>Parameters:</strong></p>

<ul>
<li>key: 键名</li>
<li>value: 键值</li>
</ul>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:destroy"></a>
              <h3>Session:destroy ()</h3>
            </dt>

            <dd class="member_method">

<p>删除 session</p>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:refresh"></a>
              <h3>Session:refresh ()</h3>
            </dt>

            <dd class="member_method">

<p>刷新 session 过期时间</p>

            </dd>


            <dt class="member_method">
              -&nbsp;&nbsp;<a name="anchor_Session:save"></a>
              <h3>Session:save ()</h3>
            </dt>

            <dd class="member_method">

<p>保存 session</p>

<p><strong>Returns:</strong></p>

<ul>
<li>成功: nil</li>
<li>失败: Error</li>
</ul>

            </dd>


          </dl>

          <!-- BEGIN functions details -->


      </td> <!-- id="content" -->

      </tr>
    </table> <!-- id="main" -->

    <div id="about">
      <i>update: 2013-05-08 09:30:25, generated by <a href="http://github.com/dualface/luadocx">luadocx 1.1</a></i>
    </div> <!-- id="about" -->

  </div> <!-- id="container" -->

</body>
</html>
